/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.logic.nondb;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import model.logic.db.CommonWSFunctionality;
import model.pojo.Settings;

/**
 *
 * This class is developed to contain all non DB operations that will be used in
 * the project
 *
 * @author Maha
 */
public class CommonMethods {

    /**
     *
     * This method is used to send mail to from any mail server to any mail
     * server, just provide the required parameters
     *
     * this method is static due to needless to initialize data in constructor
     * to be used in
     *
     * @param userName
     * @param passWord
     * @param smtpName
     * @param fromMail
     * @param toMail
     * @param subject
     * @param Contents
     *
     * @return void
     *
     * @since December 16,2012
     *
     */
    public static void sendMail(String userName, String passWord, String smtpName, String fromMail, String toMail, String subject, String Contents) {
//        final String username = "noreply@globisoft.com";
//        final String password = "P@$$w0rd";

        final String username = userName;
        final String password = passWord;


        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        //props.put("mail.smtp.host", "mail.globisoft.com");
        props.put("mail.smtp.host", smtpName);
        props.put("mail.smtp.port", "25");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromMail));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toMail));
            message.setContent(Contents, "text/html; charset=utf-8");
            message.setSubject(subject);
            // message.setText(Contents);
            message.setSentDate(new Date());
            Transport.send(message);

            System.out.println("Done1");

        } catch (MessagingException e) {
            CommonWSFunctionality cf = new CommonWSFunctionality();
            Settings settings = cf.getSettings();
            sendViaGMail(settings.getAdminMail());
        }

    }

    public static void sendViaGMail(String adminMail) {
        System.out.println(adminMail);
        final String username = "globisoft.noreply@gmail.com";
        final String password = "P@$$w0rd123";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("globisoft.noreply@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(adminMail));
            message.setSubject("Mail Server Failure");
            String Contents = "Please check Mail Server Settings<br/> Seems an error Occured since someone<br/> tried to contact but this transaction <br/> got prevented due to technical issues, <br/> this is an auto Message ";
            message.setContent(Contents, "text/html; charset=utf-8");
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }


    }
}
